package br.com.java.beans;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import br.com.java.entities.Cabecalho;
import br.com.java.service.CabecalhoService;

@Component
@Scope("session")
public class CabecalhoBean implements Serializable{
	
	private static final long serialVersionUID = -4672864882212841260L;
	
	private String conteudoCabecalho;
	private Cabecalho cabecalho;

    @Autowired
    private CabecalhoService cabecalhoService;
 
    @PostConstruct
    private void init() {
    	
    	cabecalho = new Cabecalho();
    	cabecalho = cabecalhoService.buscarUltimoRegistro();
    	
    	if (cabecalho.getConteudo() == null){
			cabecalho.setConteudo("Informação referente ao cabeçalho.");
		}
    }
    
    public String salvar(){
		cabecalhoService.saveOrUpdate(cabecalho);
		message(FacesMessage.SEVERITY_INFO, "Cabeçalho salvo com sucesso!");
		return "";
	}
    
    private void message(Severity severity, String msg) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, msg, ""));
    }

	public String getConteudoCabecalho() {
		return conteudoCabecalho;
	}

	public void setConteudoCabecalho(String conteudoCabecalho) {
		this.conteudoCabecalho = conteudoCabecalho;
	}

	public Cabecalho getCabecalho() {
		return cabecalho;
	}

	public void setCabecalho(Cabecalho cabecalho) {
		this.cabecalho = cabecalho;
	}
}
